<?php
class Arm_Controller_CliAction extends Arm_Controller_Action
{
    public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array())
    {
        parent::__construct($request, $response, $invokeArgs);
         if(Arm_Core::isCliRequest() === false) {
            throw new Arm_Exception(Arm_Translate::_('Только консольные запросы'), 404);
        }
    }
    
    
    /**
     * Вывод помощи о текущем cli контролере.
     * 
     * @param string $methodName
     * @param array $args
     */
    public function __call($methodName, $args)
    {
        $classCurent = get_class($this); 
        $class = new Zend_Reflection_Class($classCurent);
        $methodName = str_replace('Action', '', $methodName);
        $this->getResponse()->setBody(Arm_Translate::_('Не существует метода "%s", в контролере "%s"', array($methodName, $classCurent)) . "\n");
        /*@var $method Zend_Reflection_Method */
        $table = new Zend_Text_Table(array('columnWidths' => array(15, 100)));
        $table->appendRow(array('Method',  'Coment'));
        foreach ($class->getMethods(-1) as $method)
        {
            $name   = $method->getName();
            if(stripos($name, 'action') === false or $method->getDeclaringClass()->getName() !== $classCurent) {
                continue;
            }
            
            $table->appendRow(array(str_replace('Action', '', $name), $this->_filterComent($method->getDocComment())));   
        }
        $this->getResponse()->appendBody($table);
    }
    
    /**
     * Форматирования коентарий.
     * 
     * @param string $coment
     * @return string
     */
    protected function _filterComent($coment)
    {
        $coment = preg_replace('~^[\s]*\*[\s]*~mu', '', $coment);
        $coment = preg_replace('~^[\s]*(\*|/[\*]+|/)[\s]*~mu', '', $coment);
        return $coment;
    }
}